<?php

declare(strict_types=1);

namespace app\common\model;

use think\facade\Db;

class PracticeBank extends Base
{

    protected $name = 'practice_bank';
    /**
     * 生成随机考题
     */
    public static function createBank($param, $user, $crate_type = 1)
    {
        try {
            if ($crate_type == 1) {
                return ['code' => 1, 'msg' => '生成成功！'];
            }
            parent::destroy(['practice_id' => $param['id']], true);
            PracticeAnswer::destroy(['practice_id' => $param['id']], true);
            $radio_num = $param['radio_num'];
            $check_num = $param['check_num'];
            if ($radio_num > 0) {
                $radio_count = Exam::where(['types'=>1,'cate'=>$user->cate])->count();
                if ($radio_num > $radio_count) {
                    return ['code' => -1, 'msg' => '单选题不足！'];
                }
                $radio_id = Exam::where(['types'=>1,'cate'=>$user->cate])
                    ->field('DISTINCT id') // 如果需要获取特定字段，使用DISTINCT
                    ->limit($radio_num)
                    ->orderRaw('rand()')->column('id');

                foreach ($radio_id as $key => $value) {
                    $radiodata[] = [
                        'uid' => $user->id,
                        'exam_id' => $value,
                        'practice_id' => $param['id'],
                        'types' => 1,
                        'create_time' => time(),
                        'update_time' => time(),
                    ];
                }
                if (!empty($radiodata)) {
                    parent::insertAll($radiodata);
                }
            }
            if ($check_num > 0) {
                $check_count = Exam::where(['types'=>2,'cate'=>$user->cate])->count();
                if ($check_num > $check_count) {
                    return ['code' => -1, 'msg' => '多选题不足！'];
                }
                $check_id = Exam::where(['types'=>2,'cate'=>$user->cate])
                    ->field('DISTINCT id') // 如果需要获取特定字段，使用DISTINCT
                    ->limit($check_num)
                    ->orderRaw('rand()')->column('id');

                foreach ($check_id as $key => $value) {
                    $checkdata[] = [
                        'uid' => $user->id,
                        'exam_id' => $value,
                        'practice_id' => $param['id'],
                        'types' => 2,
                        'create_time' => time(),
                        'update_time' => time(),
                    ];
                }
                if (!empty($checkdata)) {
                    parent::insertAll($checkdata);
                }
            }
            return ['code' => 1, 'msg' => '生成成功！'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
    // public static function createBank($param,$user,$crate_type = 2)
    // {
    //     try {
    //         if($crate_type == 1){
    //             return ['code'=>1,'msg'=>'生成成功！'];
    //         }
    //         $radio_num = $param['radio_num'];
    //         $check_num = $param['check_num'];
    //         $radio_count = parent::where(['practice_id'=>$param['id'],'uid'=>$user->id,'types'=>1])->count();//考卷里的单选
    //         $check_count = parent::where(['practice_id'=>$param['id'],'uid'=>$user->id,'types'=>2])->count();//考卷里的多选
    //         $exam_id = parent::where(['uid'=>$user->id])->column('exam_id');//已经生成的考题
    //         $radio_id = Exam::where('types', 1)
    //         ->where('id', 'not in', $exam_id)
    //         ->field('DISTINCT id') // 如果需要获取特定字段，使用DISTINCT
    //         ->limit($radio_num)
    //         ->orderRaw('rand()')->column('id');

    //         $check_id = Exam::where('types', 2)
    //         ->where('id', 'not in', $exam_id)
    //         ->field('DISTINCT id') // 如果需要获取特定字段，使用DISTINCT
    //         ->limit($radio_num)
    //         ->orderRaw('rand()')->column('id');

    //         if($radio_num !=count($radio_id)){
    //             return ['code'=>-1,'msg'=>'单选题不足！'];
    //         }
    //         if($check_num !=count($check_id)){
    //             return ['code'=>-1,'msg'=>'多选题不足！'];
    //         }
    //         if($radio_count == 0){
    //             foreach ($radio_id as $key => $value) {
    //                 $radiodata[]=[
    //                     'uid'=>$user->id,
    //                     'exam_id'=>$value,
    //                     'practice_id'=>$param['id'],
    //                     'types'=>1,
    //                 ];
    //             }
    //             if(!empty($radiodata)){
    //                 parent::insertAll($radiodata);
    //             }
    //         }
    //         if($check_count == 0){
    //             foreach ($check_id as $key => $value) {
    //                 $checkdata[]=[
    //                     'uid'=>$user->id,
    //                     'exam_id'=>$value,
    //                     'practice_id'=>$param['id'],
    //                     'types'=>2,
    //                 ];
    //             }
    //             if(!empty($checkdata)){
    //                 parent::insertAll($checkdata);
    //             }
    //         }
    //         return ['code'=>1,'msg'=>'生成成功！'];
    //     }catch (\Exception $e) {
    //         return ['code'=>-1,'msg'=>$e->getMessage()];
    //     }
    // }
}
